package com.ForeHome.app.Service.Impl;

import com.ForeHome.app.Mapper.CategoryMapper;
import com.ForeHome.app.Mapper.ProductMapper;
import com.ForeHome.app.Service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tamll.Type.DataType;
import com.tamll.bean.Category;
import com.tamll.bean.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public Page<Category> listAll() {
        Page<Category> page=new Page<>();
        page.setCurrent(1);
        page.setSize(16);
        return categoryMapper.selectPage(page,null);
    }

    @Override
    public List<Category> listCategoryName() {
        return categoryMapper.selectList(null);
    }



    @Override
    public Category selectCategoryName(Integer productId) {
//        根据id拿到分类id
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
//        Product product = productMapper.selectOne(queryWrapper);
        List<Product> products = productMapper.selectList(queryWrapper);

//        根据分类id 查询分类名称
        QueryWrapper<Category> queryWrapper1=new QueryWrapper<>();
        queryWrapper.eq("category_id",products.get(0).getProductCategoryId());
        List<Category> list = categoryMapper.selectList(queryWrapper1);
        return list.get(0);
    }

}
